package com.byfan.barter.controller;

import com.byfan.barter.entity.Commodity;
import com.byfan.barter.service.CommodityService;
import com.byfan.barter.utils.Message;
import com.byfan.barter.utils.MessageUtil;
import com.byfan.barter.utils.MyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: FBY
 * @Description
 * @Version 1.0
 * @Date: 2021/5/17 10:44
 */
@Api(description = "商品接口")
@RestController
@RequestMapping(value = "/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @ApiOperation("获取所有商品")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Message findAll(){
        List<Commodity> result = commodityService.getAll();
        return MessageUtil.success(result);
    }

    @ApiOperation("添加商品")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Message save(Commodity commodity){
        Commodity c = commodityService.save(commodity);
        return MessageUtil.success("添加商品成功",c);
    }

    @ApiOperation("修改商品")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Message update(Commodity commodity){
        Commodity c = commodityService.getById(commodity.getId());
        MyUtil.copyPropertiesIgnoreNull(commodity,c);
        return MessageUtil.success("修改商品成功",commodityService.save(c));
    }

    @ApiOperation("删除商品")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Message delete(Integer id){
        commodityService.delete(id);
        return MessageUtil.success("删除成功");
    }

    @ApiOperation("根据id获取商品")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public Message getById(Integer id){
        Commodity commodity = commodityService.getById(id);
        return MessageUtil.success(commodity);
    }

    @ApiOperation("根据用户openId获取商品")
    @RequestMapping(value = "/getByOpenId",method = RequestMethod.GET)
    public Message getByOpenId(String openId){
        Commodity commodity = new Commodity();
        commodity.setOpenId(openId);
        List<Commodity> result = commodityService.getByExample(commodity);
        return MessageUtil.success(result);
    }

    @ApiOperation("根据用户openId和商品状态获取商品")
    @RequestMapping(value = "/getByOpenIdAndStatus",method = RequestMethod.GET)
    public Message getByOpenIdAndStatus(String openId,String status){
        Commodity commodity = new Commodity();
        commodity.setOpenId(openId);
        commodity.setStatus(status);
        List<Commodity> result = commodityService.getByExample(commodity);
        return MessageUtil.success(result);
    }

    @ApiOperation("根据商品关键字获取商品")
    @RequestMapping(value = "/getByTitle",method = RequestMethod.GET)
    public Message getByTitle(String title){
        Commodity commodity = new Commodity();
        commodity.setTitle(title);
        List<Commodity> result = commodityService.getByExample(commodity);
        return MessageUtil.success(result);
    }

    @ApiOperation("根据商品关键字获取商品")
    @RequestMapping(value = "/getByClassify",method = RequestMethod.GET)
    public Message getByClassify(String classify){
        Commodity commodity = new Commodity();
        commodity.setClassify(classify);
        List<Commodity> result = commodityService.getByExample(commodity);
        return MessageUtil.success(result);
    }

    @ApiOperation("根据地址关键字获取商品")
    @RequestMapping(value = "/getByAddress",method = RequestMethod.GET)
    public Message getByAddress(String address){
        Commodity commodity = new Commodity();
        commodity.setAddress(address);
        List<Commodity> result = commodityService.getByExample(commodity);
        return MessageUtil.success(result);
    }

    @ApiOperation("根据学校获取商品")
    @RequestMapping(value = "/getBySchool",method = RequestMethod.GET)
    public Message getBySchool(String school){
        Commodity commodity = new Commodity();
        commodity.setSchool(school);
        List<Commodity> result = commodityService.getByExample(commodity);
        return MessageUtil.success(result);
    }














}
